//
//  ReadViewController.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ReadViewController.h"

@interface ReadViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *dataArr;

// 保存第二种三张图片的cell
@property (nonatomic, retain)NSMutableArray *threePicArr;

@end

@implementation ReadViewController
- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_threePicArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    // 导航栏上按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"阅读";
    
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    // 数据处理
    [self data];
    
    
    [_tableView registerClass:[ReadCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView registerClass:[ThreePicReadCell class] forCellReuseIdentifier:@"threePiccell"];
    
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self headerData];
        [self.tableView.mj_header endRefreshing];
    }];
    self.tableView.mj_header = header;
    // 加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self footerData];
        [self.tableView.mj_footer endRefreshing];
    }];
    self.tableView.mj_footer = footer;
    
}
// 数据处理
- (void)data
{
    NSString *str = @"http://www.weixinkd.com/v3/article/lists2.json?catid=0&min_time=1454066491&step=3&iid=7003672&access=WIFI&channel=c1054&app_version=2.0.1&version_code=201&device_platform=android&os_version=LRX21V.N9006ZCUGOH2&os_api=21&device_model=SM-N9006&request_time=1454066538&uuid=359786057270002&openudid=c52e52b1a6506d1b&uid=9946707&phone_sim=2&sign=8d5af715df572b4047c9a9450548d426";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        
        NSArray *arr = result[@"items"];
        self.dataArr = [ReadModel modelHandleWithArray:arr];
        [self.tableView reloadData];
    }];
}

- (void)headerData
{
    NSString *str = @"http://www.weixinkd.com/v3/article/lists2.json?catid=0&min_time=1454066491&step=3&iid=7003672&access=WIFI&channel=c1054&app_version=2.0.1&version_code=201&device_platform=android&os_version=LRX21V.N9006ZCUGOH2&os_api=21&device_model=SM-N9006&request_time=1454066538&uuid=359786057270002&openudid=c52e52b1a6506d1b&uid=9946707&phone_sim=2&sign=8d5af715df572b4047c9a9450548d426";
    [AFHTTPHandler GETWithURL:str completion:^(id result) {
        
        NSArray *arr = result[@"items"];
        self.dataArr = [ReadModel modelHandleWithArray:arr];
        [self.tableView reloadData];
    }];
}
// 加载
- (void)footerData
{
    static NSInteger t = 2;
    NSString *str = [NSString stringWithFormat:@"http://www.weixinkd.com/v3/article/lists2.json?catid=0&min_time=1454066491&step=3&iid=7003672&access=WIFI&channel=c1054&app_version=2.0.1&version_code=201&device_platform=android&os_version=LRX21V.N9006ZCUGOH2&os_api=21&device_model=SM-N9006&request_time=1454066538&uuid=359786057270002&openudid=c52e52b1a6506d1b&uid=9946707&phone_sim=2&sign=8d5af715df572b4047c9a9450548d426"];
    NSString *urlStr = [str stringByReplacingOccurrencesOfString:@"step=3" withString:[NSString stringWithFormat:@"step=%ld", t]];
    [AFHTTPHandler GETWithURL:urlStr completion:^(id result) {
        NSArray *arr = [ReadModel modelHandleWithArray:result[@"items"]];
        for (NSInteger i = 0; i < arr.count; i++) {
            [_dataArr addObject:arr[i]];
        }
        [_tableView reloadData];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *m = _dataArr[indexPath.row];
    // imgnewextra数组中没有值就是第一种cell
    if (m.extra.count == 0) {
        ReadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.thumb] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.title.text = m.title;
        cell.source.text = m.account_name;
        return cell;
        
    }else{
        ThreePicReadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"threePiccell" forIndexPath:indexPath];
        // 左侧第一张图片
        [cell.oneImage sd_setImageWithURL:[NSURL URLWithString:m.extra[0]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        // 右侧第二张图片
        [cell.twoImage sd_setImageWithURL:[NSURL URLWithString:m.extra[1]] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        // 右侧第三张图片
        [cell.threeImage sd_setImageWithURL:[NSURL URLWithString:m.extra[2]] placeholderImage:[UIImage imageNamed:@""]];
        cell.title.text = m.title;
        cell.source.text = m.account_name;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *m = _dataArr[indexPath.row];
    if (m.extra.count == 0) {
        return HEIGHT * 150;
    }else{
        return HEIGHT * 165;
    }
}
// 阅读详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadModel *m = _dataArr[indexPath.row];
    ReadSelectVC *readSelect = [[ReadSelectVC alloc] init];
    // 传递网址
    readSelect.str = m.url;
    // 传递title
    readSelect.urlTitle = m.title;
    [self.navigationController pushViewController:readSelect animated:YES];
    [readSelect release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
